<?php

	// +----------------------------------------------------------------------
	// | 认证提交
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\model\store;


	class StoreAddress extends Common
	{

		/**
		 * 写入修改
		 * @param $data
		 */
		public static function onAfterWrite($data)
		{
			$has = self::where('store_id', $data['store_id'])
				->where('id', '<>', $data['id'])
				->where('status', 1)
				->find();
			if (1 == $data['is_default'] && $has) {
				self::where('store_id', $data['store_id'])
					->where('id', '<>', $data['id'])
					->where('is_default', 1)
					->where('status', 1)
					->update(['is_default' => 0]);
			}
		}


		/**
		 * 写入修改
		 * @param $data
		 */
		public static function onBeforeWrite($data)
		{
//			$data['address'] = Event::trigger('china_city_info', $data, true);;
			$data['is_default'] = 1;
			$data['address'] = Region::where('id',$data['district_id'])->value('name');
		}

		public function store()
		{
			return $this->belongsTo(Store::class);
		}

		protected $type = [];
	}
